# Milk butter ratioes to prices
#
# Date start:   2022-11-18
# Auhtor:       Christian Vedel 
# Purpose:      This script contains a function which converts changes in
#               MB ratio to change in value of Butter outputted for the median
#               creamery

# ==== Libraries ====
library(tidyverse)

# ==== Auxilliary data ====
prices = read.csv2("Converting MB ratios to real value/Priceindex_dst.csv")

# Changing reference year to 2010
price2010 = prices %>% filter(Year == 2010)
prices = prices %>%
  mutate(
    Index = Index/price2010$Index
  ) %>% select(-Source)

usd_per_dkk = 1/5.625670 # In 2010 according to the national bank of Denmark

# ==== Auxilliary funcitons ====
deltaButter = function(Delta_MB, Milk, MB_ratio){
  Milk * (-Delta_MB) / (MB_ratio*(MB_ratio + Delta_MB))
}

# ==== MB_to_USD ====
# Delta_MB:         Change in MB ratio
# data0:            Data for which this should be estimated. Should contain: 
#                     - Year
#                     - Milk
#                     - MB_ratio
#                     - Price_butter

MB_to_USD = function(
  Delta_MB,
  data0,
  only_median = TRUE
  ){
  
  test = all(c("Year", "Milk", "MB_ratio", "Price_butter") %in% names(data0))
  if(!test) stop("Some variables are missing in data0")
  
  # Estimating change for median
  res_median = data0 %>% 
    mutate(
      Turnover = Price_butter*Butter/100 # To get DKK instead Øre
    ) %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    summarise(
      Milk = median(Milk, na.rm = TRUE),
      MB_ratio = median(MB_ratio, na.rm = TRUE),
      Price_butter = median(Price_butter, na.rm = TRUE)/100, # To get DKK instead Øre
      Year = median(Year),
      Turnover = median(Turnover, na.rm = TRUE),
      Dividends = median(Dividends, na.rm = TRUE)
    ) %>% 
    mutate(
      Delta_butter = deltaButter(Delta_MB, Milk, MB_ratio)
    ) %>% 
    mutate(
      Delta_nom_DKK = Delta_butter * Price_butter
    ) %>% 
    mutate(
      Delta_nom_DKK_of_turnover = Delta_nom_DKK / Turnover,
      Delta_nom_DKK_of_dividends = Delta_nom_DKK / Dividends
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_nom_DKK/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  if(only_median){
    return(res_median)
  }
  
  # Estimating for all
  res_all = data0 %>% 
    mutate(
      Turnover = Price_butter*Butter/100 # To get DKK instead Øre
    ) %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    mutate(
      Price_butter = Price_butter/100, # To get DKK instead Øre
    ) %>% 
    mutate(
      Delta_butter = deltaButter(Delta_MB, Milk, MB_ratio)
    ) %>% 
    mutate(
      Delta_nom_DKK = Delta_butter * Price_butter
    ) %>% 
    mutate(
      Delta_nom_DKK_of_turnover = Delta_nom_DKK / Turnover
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_nom_DKK/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  # Plotting it
  p1 = res_all %>% 
    mutate(Delta_real_USD = Delta_real_USD/1000) %>% 
    ggplot(aes(Delta_real_USD)) + 
    geom_histogram() +
    theme_bw() + 
    labs(
      x = "Implied change in value outputted\n(In thousands of 2010 USD)"
    )  
  
  return(
    list(
      res_all = res_all,
      plot = p1
    )
  )
    
}


# ==== Butter to USD ====
lButter_to_USD = function(
    Delta_logButter,
    data0,
    only_median = TRUE
){
  
  test = all(c("Year", "Milk", "MB_ratio", "Price_butter") %in% names(data0))
  if(!test) stop("Some variables are missing in data0")
  
  # Estimating change for median
  res_median = data0 %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    summarise(
      Butter = median(Butter, na.rm = TRUE),
      Price_butter = median(Price_butter, na.rm = TRUE)/100, # To get DKK instead Øre
      Year = median(as.numeric(as.character(Year))),
      Dividends = median(Dividends, na.rm = TRUE)
    ) %>% 
    mutate(
      Delta_butter = Delta_logButter*Butter
    ) %>% 
    mutate(
      Delta_nom_DKK = Delta_butter * Price_butter
    ) %>% 
    mutate(
      Delta_nom_DKK_of_dividends = Delta_nom_DKK / Dividends
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_nom_DKK/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  if(only_median){
    return(res_median)
  }
  
  # Estimating for all
  res_all = data0 %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    mutate(
      Price_butter = Price_butter/100, # To get DKK instead Øre
    ) %>% 
    mutate(
      Delta_butter = Delta_logButter*Butter
    ) %>% 
    mutate(
      Delta_nom_DKK = Delta_butter * Price_butter
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_nom_DKK/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  # Plotting it
  p1 = res_all %>% 
    mutate(Delta_real_USD = Delta_real_USD/1000) %>% 
    ggplot(aes(Delta_real_USD)) + 
    geom_histogram() +
    theme_bw() + 
    labs(
      x = "Implied change in value outputted\n(In thousands of 2010 USD)"
    )  
  
  return(
    list(
      res_all = res_all,
      plot = p1
    )
  )
  
}

# ==== Dividends to USD ====
lDiv_to_USD = function(
    Delta_logDividends,
    data0,
    only_median = TRUE
){
  
  test = all(c("Year", "Milk", "MB_ratio", "Price_butter") %in% names(data0))
  if(!test) stop("Some variables are missing in data0")
  
  # Estimating change for median
  res_median = data0 %>% 
    mutate(
      Turnover = Price_butter*Butter/100 # To get DKK instead Øre
    ) %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    summarise(
      Dividends = median(Dividends, na.rm = TRUE),
      Price_butter = median(Price_butter, na.rm = TRUE)/100, # To get DKK instead Øre
      Year = median(as.numeric(as.character(Year))),
      Turnover = median(Turnover, na.rm = TRUE)
    ) %>% 
    mutate(
      Delta_div = Delta_logDividends*Dividends
    ) %>% 
    mutate(
      Delta_div_of_turnover = Delta_div/Turnover
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_div/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  if(only_median){
    return(res_median)
  }
  
  # Estimating for all
  res_all = data0 %>% 
    mutate(
      Turnover = Price_butter*Butter/100 # To get DKK instead Øre
    ) %>% 
    mutate(Year = as.numeric(as.character(Year))) %>% 
    mutate(
      Price_butter = Price_butter/100, # To get DKK instead Øre
    ) %>% 
    mutate(
      Delta_butter = Delta_logButter*Butter
    ) %>% 
    mutate(
      Delta_nom_DKK = Delta_butter * Price_butter
    ) %>% 
    left_join(prices, by = "Year") %>% 
    mutate(
      Delta_real_DKK = Delta_nom_DKK/Index
    ) %>% 
    mutate(
      Delta_real_USD = Delta_real_DKK * usd_per_dkk
    )
  
  # Plotting it
  p1 = res_all %>% 
    mutate(Delta_real_USD = Delta_real_USD/1000) %>% 
    ggplot(aes(Delta_real_USD)) + 
    geom_histogram() +
    theme_bw() + 
    labs(
      x = "Implied change in value outputted\n(In thousands of 2010 USD)"
    )  
  
  return(
    list(
      res_all = res_all,
      plot = p1
    )
  )
  
}
